package com.umeng.analytics.pro;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.os.Bundle;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

public class ap {
    public static String a;
    Application.ActivityLifecycleCallbacks b;
    private final Map c;
    private static JSONObject d;
    private Application e;

    static {
        ap.a = null;
        ap.d = new JSONObject();
    }

    public ap(Activity arg2) {

        this.c = new HashMap();
        this.e = null;
        this.b = new Application.ActivityLifecycleCallbacks() {
            public void onActivityCreated(Activity arg1, Bundle arg2) {
            }

            public void onActivityDestroyed(Activity arg1) {
            }

            public void onActivityPaused(Activity arg2) {
                ap.b(ap.this, arg2);
            }

            public void onActivityResumed(Activity arg2) {
                ap.a(ap.this, arg2);
            }

            public void onActivitySaveInstanceState(Activity arg1, Bundle arg2) {
            }

            public void onActivityStarted(Activity arg1) {
            }

            public void onActivityStopped(Activity arg1) {
            }
        };
        if (arg2 != null) {
            this.e = arg2.getApplication();
            this.a(arg2);
        }
    }

    public void a(Context arg2) {
        this.c(null);
        this.a();
    }

    static void a(ap arg0, Activity arg1) {
        arg0.b(arg1);
    }

    private void a(Activity arg3) {
        this.e.registerActivityLifecycleCallbacks(this.b);
        if (ap.a == null) {
            this.b(arg3);
        }
    }

    public void a() {
        if (this.e != null) {
            this.e.unregisterActivityLifecycleCallbacks(this.b);
        }
    }

    public static void b(Context arg5) {
        JSONObject v1;
        v1 = ap.d;
        synchronized (v1) {
            if (ap.d.length() > 0) {
                w.a(arg5).a(bd.a(), ap.d, w.w$a.AUTOPAGE);
                ap.d = new JSONObject();
            }
        }
    }

    static void b(ap arg0, Activity arg1) {
        arg0.c(arg1);
    }

    private void b(Activity arg7) {
        ap.a = arg7.getPackageName() + "." + arg7.getLocalClassName();
        Map v1 = this.c;
        synchronized (v1) {
            this.c.put(ap.a, Long.valueOf(System.currentTimeMillis()));
        }
    }

    private void c(Activity arg7) {
        JSONObject v2_1;
        Map v2;
        long v0 = 0;
        try {
            v2 = this.c;
            synchronized (v2) {
                if (this.c.containsKey(ap.a)) {
                    v0 = System.currentTimeMillis() - ((Long) this.c.get(ap.a)).longValue();
                    this.c.remove(ap.a);
                }
            }
            v2_1 = ap.d;
            synchronized (v2_1) {
                JSONObject v3 = new JSONObject();
                ap.d = v3;
                v3.put("page_name", ap.a);
                ap.d.put("duration", v0);
            }
        } catch (Throwable v0_1) {

        }
    }
}

